<template>
  <div>
    <el-dialog
      title="审核任务"
      :visible.sync="dialogVisible"
      :modal="true"
      :appendToBody="true"
      width="500px"
      v-dialog-drag
      :close-on-click-modal="false"
      top="5vh"
      :destroy-on-close="true"
      @close="handleClose()"
      @open="handleOpen()"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="审核结果" prop="result">
              <el-radio-group v-model="form.result">
                <el-radio label="1">同意</el-radio>
                <el-radio label="2">驳回</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="审核意见" prop="opinion">
              <el-input
                v-model="form.opinion"
                type="textarea"
                :rows="4"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          v-if="canEdit"
          type="primary"
          :loading="saveloading"
          @click="handleSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { checkPermi } from "@/utils/permission";
import { saveTask } from "@/api/taskMgmt/center";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    rows: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      loading: false,
      form: {
        result: null,
        opinion: null,
      },
      rules: {
        result: [
          { required: true, message: "请选择审核结果", trigger: "change" },
        ],
        opinion: [
          {
            max: 200,
            message: "审核意见字段长度最大为200个字符",
            trigger: "change",
          },
        ],
      },
      saveloading: false,
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val);
      },
      get() {
        return this.visible;
      },
    },
    canEdit() {
      if (this.rowIds?.length) {
        return checkPermi(["taskMgmt:center:examine"]);
      }
      return true;
    },
    rowIds() {
      if (!this.rows?.length) {
        return [];
      }
      const item = this.rows[0];
      if (typeof item == "string") {
        return this.rows;
      }
      return this.rows.map((ele) => ele.id);
    },
  },
  methods: {
    handleClose() {
      this.$refs.ruleForm.resetFields();
    },
    handleOpen() {},
    handleSubmit() {
      if (!this.rowIds?.length) {
        return;
      }
      this.$refs.ruleForm.validate((flag) => {
        if (flag) {
          this.saveloading = true;
          const param = {
            ids: this.rowIds,
            operateType: "audit",
            auditResult: this.form.result,
            auditOpinion: this.form.opinion,
          };
          saveTask(param)
            .then((res) => {
              this.msgSuccess("操作成功！");
              this.$emit("refresh");
              this.dialogVisible = false;
            })
            .finally(() => {
              this.saveloading = false;
            });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
